package com.fyzk.activity.forum;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tyjc.lottery.R;
import com.fyzk.utils.ScreenUtils;

import java.text.SimpleDateFormat;

/**
 * Created by fanxs11 on 17/5/10.
 */

public class MyPostAdapter extends BaseAdapter {

    private Context context;

    public MyPostAdapter(Context context) {
        this.context = context;
    }


    @Override
    public int getCount() {
        return ForumMyPostActivity.infos.size();
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Object getItem(int i) {
        return ForumMyPostActivity.infos.get(i);
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

        HolderView holderView;
        TopicListBean info = ForumMyPostActivity.infos.get(position);
        if (view == null) {
            holderView = new HolderView();
            view = LayoutInflater.from(context).inflate(R.layout.adapter_mypost, null);
            holderView.imgs_ll = (LinearLayout) view.findViewById(R.id.mypost_item_imgs);
            holderView.time_tv = (TextView) view.findViewById(R.id.mypost_item_time);
            holderView.title_tv = (TextView) view.findViewById(R.id.mypost_item_title);
            holderView.content_tv = (TextView) view.findViewById(R.id.mypost_item_content);
            holderView.from_tv = (TextView) view.findViewById(R.id.mypost_item_from);
            holderView.reply_tv = (TextView) view.findViewById(R.id.mypost_item_reply);
            view.setTag(holderView);
        } else {
            holderView = (HolderView) view.getTag();
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        holderView.time_tv.setText(format.format(info.getAdd_time() * 1000));
        holderView.title_tv.setText(info.getQuestion_content());
        try {
            holderView.from_tv.setText("发表于 " + info.getTopics().get(0).getTopic_title());
            holderView.reply_tv.setText(info.getAnswer_count() + "回贴");
        } catch (Exception e) {

        }

        String text = info.getQuestion_detail();
        holderView.imgs_ll.removeAllViews();
        if (text != null && text.contains("[/attach]")) {       // 内容中带图片

            if (info.getImageBeens() != null) {
                for (int i = 0; i < 3; i++) {
                    ImageView imageView = new ImageView(context);
                    int margin = 10;
                    int width = ScreenUtils.getInstance().getWidth() / 3 - margin * 2;
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
                            (LinearLayout.LayoutParams.MATCH_PARENT, width);
                    layoutParams.setMargins(margin, margin, margin, margin);
                    layoutParams.weight = 1;
                    imageView.setLayoutParams(layoutParams);
                    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    if (i < info.getImageBeens().size()) {
                        Glide.with(context).load(info.getImageBeens().get(i).thumb).into(imageView);
                    }
                    holderView.imgs_ll.addView(imageView);
                }
            }

        }

        String html = getHtmlText(info);
        if (html != null) {
            holderView.content_tv.setText(Html.fromHtml(html, new MImageGetter(holderView
                    .content_tv, context), null));
        }

        return view;
    }

    private String getHtmlText(TopicListBean headInfo) {
        String text = headInfo.getQuestion_detail();
        String htmlText = "";
        if (text.contains("[attach]")) {
            String[] contents = text.split("\\[/attach]");
            for (int i = 0; i < contents.length; i++) {
                String[] t_i = contents[i].split("\\[attach]");
                if (t_i[0] != null && !t_i[0].equals("")) {
                    htmlText += t_i[0];
                }
            }
        } else {
            htmlText = text;
        }
        if (htmlText.contains("\n")) {
            htmlText = htmlText.replaceAll("\n", " ");
        }
        return htmlText;
    }

    class HolderView {
        TextView time_tv;
        LinearLayout imgs_ll;
        TextView title_tv;
        TextView content_tv;
        TextView from_tv;
        TextView reply_tv;
    }
}
